﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Lottery.MediaData.Core {
    public static class FTHelper {
        public static long CharToLong(char c) {
            var d = new Dictionary<char, long>();
            d['3'] = 4L;
            d['1'] = 2L;
            d['0'] = 1L;
            d['#'] = 0L; //九场
            return d[c];
        }

        public static long CombinCharToLong(this string s) {
            var result = 0L;
            foreach (var ch in s) {
                result |= CharToLong(ch);
            }
            return result;
        }

        public static long CombinCharToLongAndThenNot(this string s) {
            var result = s.CombinCharToLong();
            result = (~result) & 7L;
            return result;
        }

        public static long StrToLong(string s) {
            var d = new Dictionary<char, long>();
            d['3'] = 4L;
            d['1'] = 2L;
            d['0'] = 1L;
            d['#'] = 0L; //九场
            long result = 0;
            for (int i = 0; i < 14; i++) {
                var c = s[i];
                result |= d[c] << (13 - i) * 3;
            }

            return result;
        }

        public static string LongToStr(long ln) {
            var sb = new StringBuilder();
            var d = new Dictionary<long, char>();
            d[4L] = '3';
            d[2L] = '1';
            d[1L] = '0';
            d[0L] = '#'; //九场
            for (int i = 13; i >= 0; i--) {
                var pos = (ln & ((1L | 2L | 4L) << (i * 3))) >> (i * 3);
                sb.Append(d[pos]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 产生14位全集
        /// </summary>
        public static List<long> GenAllSet() {
            var result = new List<long>();
            var d = new long[] { 1L, 2L, 4L };

            #region 14层循环
            for (int a1 = 0; a1 < 3; a1++) {
                for (int a2 = 0; a2 < 3; a2++) {
                    for (int a3 = 0; a3 < 3; a3++) {
                        for (int a4 = 0; a4 < 3; a4++) {
                            for (int a5 = 0; a5 < 3; a5++) {
                                for (int a6 = 0; a6 < 3; a6++) {
                                    for (int a7 = 0; a7 < 3; a7++) {
                                        for (int a8 = 0; a8 < 3; a8++) {
                                            for (int a9 = 0; a9 < 3; a9++) {
                                                for (int a10 = 0; a10 < 3; a10++) {
                                                    for (int a11 = 0; a11 < 3; a11++) {
                                                        for (int a12 = 0; a12 < 3; a12++) {
                                                            for (int a13 = 0; a13 < 3; a13++) {
                                                                for (int a14 = 0; a14 < 3; a14++) {
                                                                    long ln = d[a1] << 13 * 3 | d[a2] << 12 * 3 | d[a3] << 11 * 3 |
                                                                        d[a4] << 10 * 3 | d[a5] << 9 * 3 | d[a6] << 8 * 3 | d[a7] << 7 * 3 |
                                                                        d[a8] << 6 * 3 | d[a9] << 5 * 3 | d[a10] << 4 * 3 | d[a11] << 3 * 3 |
                                                                        d[a12] << 2 * 3 | d[a13] << 3 | d[a14];
                                                                    result.Add(ln);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            return result;
        }

        public static List<long> DoFilter(FTCellData ft, List<long> touZhu) {
            var result = touZhu;
            var resultStatus = new List<bool>();
            for (int i = 0; i < result.Count; i++) {
                resultStatus.Add(true);
            }

            for (int row = 0; row < ft.RowCount; row++) {
                var flag = ft.GetFtFlags(row);
                var up = ft.GetUp(row);
                var low = ft.GetLow(row);
                for (int index = 0; index < result.Count; index++) {
                    if (!resultStatus[index]) continue;
                    var item = result[index];

                    var d = flag & item;
                    int count = 0;
                    for (int i = 0; i < 14; i++) {
                        var t = d & (7L << ((13 - i) * 3));
                        if (t != 0L) { 
                            count++;
                            if (count > up) break;
                        }
                    }

                    resultStatus[index] = (count >= low && count <= up);
                }
            }

            var r = new List<long>();
            for (int i = 0; i < result.Count; i++) {
                if (resultStatus[i]) {
                    r.Add(result[i]);
                }
            }
            return r;
        }

        /// <summary>
        /// 14场投注表路径
        /// </summary>
        public static string TouZhuPath_14 {
            get { return System.Windows.Forms.Application.StartupPath + @"\14-用户投注表\"; }
        }

        /// <summary>
        /// 9场投注表路径
        /// </summary>
        public static string TouZhuPath_09 {
            get { return System.Windows.Forms.Application.StartupPath + @"\9-用户投注表\"; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="qiShu"></param>
        /// <param name="data"></param>
        /// <param name="lineCount"></param>
        public static string SaveTouZhu(string qiShu, List<long> data, int lineCount) {
            var line = FTHelper.LongToStr(data[0]);
            var isNine = false;
            foreach (var ch in line) {
                if (ch == '#') {
                    isNine = true;
                    break;
                }
            }

            var path = isNine ? TouZhuPath_09 : TouZhuPath_14;
            var maxCount = isNine ? 9 : 14;

            var count = (from item in Directory.GetFiles(path)
                         where Path.GetExtension(item) == ".txt"
                            && Path.GetFileNameWithoutExtension(item).Substring(6, qiShu.Length) == qiShu
                         select item).Count();

            count++;
            var fileName = string.Format("{2}用户投注表_{0}_{3}场-{1}.txt", qiShu, count, path, maxCount);

            int index = 1;
            using (var s = File.CreateText(fileName)) {
                foreach (var item in data) {
                    if (index != 1) {
                        s.Write('\t');
                    }
                    s.Write(LongToStr(item));
                    if (index++ % lineCount == 0) {
                        s.WriteLine();
                        index = 1;
                    }
                }
                s.Close();
            }

            return fileName;
        }
    }
}
